﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace QuanLyKhachSan
{
    public partial class UC_KhachHang_Edit : System.Web.UI.UserControl
    {
        private int KhachHangID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {   
                if (this.Request.QueryString["KhachHangID"] != null)
                {
                    KhachHangID = int.Parse(this.Request.QueryString["KhachHangID"].ToString());
                }
                if (!Page.IsPostBack)
                {
                    QuocTich_Load();
                    GioiTinh_Load();
                    cbxQuocTich.SelectedValue = "1";
                    if (KhachHangID != -1)
                    {
                        KhachHang_Load(); 
                    }
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        /// <summary>
        /// KhachHang_Load
        /// </summary>
        protected void KhachHang_Load()
        {
            try
            {
                KhachHangInfo item = KhachHangBLL.KhachHang_GetItem(KhachHangID);

                txtHoTen.Text = item.HoTen;
                txtDiaChi.Text = item.DiaChi;
                txtDienThoai.Text = item.DienThoai;
                txtCMND.Text = item.CMND;
                if (!string.IsNullOrEmpty(item.NgaySinh))
                    rdpNgaySinh.SelectedDate = DateTime.Parse(item.NgaySinh);
                txtNoiSinh.Text = item.NoiSinh;
                txtSoVisa.Text = item.SoVisa;
                if (!string.IsNullOrEmpty(item.ThoiHanVisa))
                    rdpThoiHanVisa.SelectedDate = DateTime.Parse(item.ThoiHanVisa);
                txtNguoiTiepNhan.Text = item.NguoiTiepNhan;
                if (!string.IsNullOrEmpty(item.NgayNhapCanh))
                    rdpNgayNhapCanh.SelectedDate = DateTime.Parse(item.NgayNhapCanh);
                if (!string.IsNullOrEmpty(item.TamTruTuNgay))
                    rdpTuNgay.SelectedDate = DateTime.Parse(item.TamTruTuNgay);
                if (!string.IsNullOrEmpty(item.TamTruDenNgay))
                    rdpDenNgay.SelectedDate = DateTime.Parse(item.TamTruDenNgay);
                cbxQuocTich.SelectedValue = item.QuocTichID.ToString();
                if (item.GioiTinh != -1)
                    cbxGioiTinh.SelectedValue = item.GioiTinh.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// QuocTich_Load
        /// </summary>
        protected void QuocTich_Load()
        {
            try
            {
                cbxQuocTich.DataSource = QuocTichBLL.QuocTich_GetItems();
                cbxQuocTich.DataTextField = "TenQuocTich";
                cbxQuocTich.DataValueField = "QuocTichID";
                cbxQuocTich.DataBind();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        /// <summary>
        /// GioiTinh_Load
        /// </summary>
        protected void GioiTinh_Load()
        {
            try
            {
                cbxGioiTinh.Items.Add(new Telerik.Web.UI.RadComboBoxItem("Nam", "0"));
                cbxGioiTinh.Items.Add(new Telerik.Web.UI.RadComboBoxItem("Nữ", "1"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                string ngaysinh = string.Empty;
                string thoihanvisa = string.Empty;
                string ngaynhapcanh = string.Empty;
                string tungay = string.Empty;
                string denngay = string.Empty;
                int gioitinh = -1;

                if (!rdpNgaySinh.IsEmpty)
                    ngaysinh = rdpNgaySinh.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!rdpThoiHanVisa.IsEmpty)
                    thoihanvisa = rdpThoiHanVisa.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!rdpNgayNhapCanh.IsEmpty)
                    ngaynhapcanh = rdpNgayNhapCanh.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!rdpTuNgay.IsEmpty)
                    tungay = rdpTuNgay.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!rdpDenNgay.IsEmpty)
                    denngay = rdpDenNgay.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!string.IsNullOrEmpty(cbxGioiTinh.SelectedValue.ToString()))
                    gioitinh = int.Parse(cbxGioiTinh.SelectedValue.ToString());

                if (KhachHangID == -1)
                {
                    KhachHangBLL.KhachHang_Add(txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, txtCMND.Text, ngaysinh, txtNoiSinh.Text, txtSoVisa.Text,
                        thoihanvisa, txtNguoiTiepNhan.Text, ngaynhapcanh, tungay, denngay, int.Parse(cbxQuocTich.SelectedValue.ToString()),
                        gioitinh, 0, DateTime.Now.ToString("MM/dd/yyyy"));
                }
                else
                {
                    KhachHangBLL.KhachHang_Edit(KhachHangID, txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, txtCMND.Text, ngaysinh, txtNoiSinh.Text, txtSoVisa.Text,
                        thoihanvisa, txtNguoiTiepNhan.Text, ngaynhapcanh, tungay, denngay, int.Parse(cbxQuocTich.SelectedValue.ToString()),
                        gioitinh, 0, DateTime.Now.ToString("MM/dd/yyyy"));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
}
}
